<!DOCTYPE html>
<html>

<head lang="en">
    <title>找攻略</title>
    <meta charset="utf-8"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <link rel="stylesheet" href="/js/bootstrap-4.1.1-dist/css/bootstrap.min.css">
    <script src="/js/jquery/jquery.min.js"></script>
    <script src="/js/bootstrap-4.1.1-dist/js/bootstrap.min.js"></script>
    <link rel="stylesheet" href="/css/font/css/font-awesome.min.css"/>
    <link rel="stylesheet" href="/css/reset.css"/>
    <link rel="stylesheet" href="/css/strategy.css"/>
    <script src="/js/plugins/jrender/jrender.min.js"></script>
    <link rel="stylesheet" href="/js/plugins/dialog2/dialog.css"/>
    <script src="/js/plugins/dialog2/dialog.min.js"></script>
    <script src="/js/common.js"></script>
    <script>
        $(function () {

            var regionId;
            //发送请求去查询  获取最近一篇的攻略推荐
            $.get("/travels/commends", {type: 3, pageSize: 1}, function (data) {
                //渲染数据
                $(".commend").renderValues(data.list[0], {//只有一条数据 没必要循环回显了
                    getHref: getHref
                });
            });

            //发送请求查询当季推荐 不用翻页 pageSize为0 状态为推荐
            $.get("/strategies", {pageSize: 0, state: 1}, function (data) {
                console.log(data);
                //渲染数据
                $(".strategyCommend").renderValues(data, {
                    getHref: getHref
                });
            });

            //发送请求状态为推荐的地区 不分页
            $.get("/regions", {state: 1}, function (data) {
                var title = ""//地区
                var content = "";//内容
                $.each(data, function (index, ele) {
                    title += '<li class="nav-item"><a class="nav-link" data-rid="' + ele.id + '" data-toggle="pill" href="#pills-' + ele.id + '">' + ele.name + '</a></li>';
                    content += '<div class="tab-pane fade" id="pills-' + ele.id + '"></div>';
                });
                //标签页的结构
                $("#pills-tab").append(title);//题目
                $("#pills-tabContent").append(content);//内容

                //给标题绑定地区标题点击事件
                $(".nav-link[data-rid]").click(function () {
                    //重置翻页参数(不同的标签页,都从当前页都从2开始)
                    currentPage = 2;//当前页
                    pages = 2;//总页数
                    //攻略标签的id
                    regionId = $(this).data("rid");
                    //发送请求,查询某个地区的攻略信息
                    $.get("/regions/" + regionId + "/strategies", function (data) {
                        $("#pills-" + regionId).html(data);
                    }, "html");
                });
            });



            

            //当前页
            var currentPage = 2;
            //总页数
            var pages = 2;
            //是否查询的标记
            var loading = false;

            //查询方法
            function query() {
                //如果正在查询就不继续查了,退出
                if (loading) {
                    return;
                }
                //设置为查询状态
                loading = true;
                //全部攻略
                $.get("/regions/" + regionId + "/strategies", {currentPage: currentPage}, function (data) {
                    console.log(data);
                    $.each(data.list, function (index, ele) {
                        //克隆模板
                        var div = $("#template .mb").clone(true);
                        //填充数据
                        $(div).find("img").attr("src", ele.coverUrl);
                        $(div).find("span").html(ele.title);
                        $(div).find("a").attr("href", "strategyCatalogs.html?id=" + ele.id)
                        //添加到指定位置
                        $(".classify").append(div);
                    });

                    //当前页+1
                    currentPage++;
                    //总页数
                    pages = data.pages;
                    //设置为没有在查询  本次查询结束
                    loading = false;
                }, "json");
            }

            //滚动页面 触发事件 翻页
            $(window).scroll(function () {
                //判断是否滚动到底部了  窗口的高度+文档滚动的高度 大于等于 文档的高度
                if ($(window).height() + $(document).scrollTop() + 1 >= $(document).height()) {
                    //如果当前页大于总页数就不查询了
                    if (currentPage <= pages && regionId) {
                        //查询
                        query();
                    } else {
                        $(document).dialog({
                            type: 'notice',
                            infoText: '亲,已经到底了',
                            autoClose: 1500,
                            position: 'center'  // center: 居中; bottom: 底部
                        });
                    }

                }
            });


            //当点击搜索框的时候清空搜索框
            $("#search").val("");
            //给搜索绑定点击事件
            $("#searchBtn").click(function () {
                //console.log($("#search").val());
                //获取搜索框中的数据
                var keyword=$("#search").val();
                //判断是否有值,有值再进行跳页面搜索
                if(keyword==null||keyword.length==0){
                    //使用提示框进行提示
                    $(document).dialog({
                        titleText:"温馨提示",
                        content:"搜索内容不能为空"
                    });
                }else{
                    //有值就进行跳转页面并将关键字数据带上
                    window.location.href="/strategySearch.html?keyword="+keyword+"&&type="+1
                }
            });

        })
    </script>

</head>

<body>

<!--分页时使用的模板-->
<div id="template" style="display: none">
    <div class="col-6 mb">
        <a>
            <img class="float-left">
            <div class="classify-text">
                <span></span>
                <p>0人收藏</p>
            </div>
        </a>
    </div>
</div>


<div class="search-head">
    <div class="row nav-search">

        <div class="col-1">
            <a href="index.html">
                <span><i class="fa fa-chevron-left fa-2x"></i></span>
            </a>
        </div>

        <div class="col-9">
            <div class="input-group-sm search">
                <input id="search" class="form-control" placeholder="搜索旅行攻略、日记和旅游资讯">
            </div>
        </div>
        <!--搜索放大镜按钮-->
        <div class="col-1">
            <i class="fa fa-search" id="searchBtn"
               style="color:white;font-size:25px;position: absolute;top: 23%;left: 88%">
            </i>
        </div>

    </div>
</div>

<ul class="nav nav-pills nav-justified border border-left-0 border-top-0 border-right-0" id="pills-tab" role="tablist">
    <li class="nav-item">
        <a class="nav-link active show" data-toggle="pill" href="#pills-top">
            <span style="font-weight: bold;">推荐</span>
        </a>
    </li>
</ul>

<div class="tab-content" id="pills-tabContent">
    <div class="tab-pane fade active show" id="pills-top">
        <!--攻略游记推荐-->
        <div class="container commend">
            <a render-fun="getHref" render-key="travelId" data-url="/travelContent.html?id=">
                <img render-src="coverUrl">
                <p render-html="title"></p>
            </a>
        </div>
        <hr>

        <div class="container strategyCommend">
            <h6>当季推荐</h6>
            <div class="row hot" render-loop="list">
                <div class="col-4">
                    <a render-fun="getHref" render-key="list.id" data-url="/strategyCatalogs.html?id=">
                        <img render-src="list.coverUrl">
                        <p render-html="list.title"></p>
                    </a>
                </div>
            </div>
        </div>
    </div>
</div>
</body>
</html>